package edu.ntu.cloudroid.client;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import edu.ntu.cloudroid.model.IDs;
import edu.ntu.cloudroid.model.UserData;
import edu.ntu.cloudroid.model.data.DetachedData;
import edu.ntu.cloudroid.model.data.LocationInfo;
import edu.ntu.cloudroid.model.data.TraceInfo;
import edu.ntu.cloudroid.model.data.VitalSignData;
import edu.ntu.cloudroid.model.data.VitalSignInfo;
import edu.ntu.cloudroid.model.friend.FriendInfo;
import edu.ntu.cloudroid.model.friend.GroupInfo;
import edu.ntu.cloudroid.model.policy.PolicyInfo;
import edu.ntu.cloudroid.model.profile.ProfileInfo;
import edu.ntu.cloudroid.nongui.com.AbstractResponse;
import edu.ntu.cloudroid.nongui.com.Communicator;
import edu.ntu.cloudroid.nongui.com.LocationRequestWithInputs;

public class TestUpload2 {
	static final String DEFAULT_DATE = "2011-01-01 00:00";
	public static void main(String[] args) throws Exception{
				
		Communicator com = new Communicator(null, "sandspcloud.appspot.com", 80);
		if (args.length>0){
			if (args[0].equals("-more"))
				addMoreVitalData(com,10,21,2);
			return;
		}
		
		//Generate profiles and upload them
		List<UserData> profiles = Generator.generateProfiles(
		//		IDs.DataID.VitalSignData.ordinal(), 10);
				IDs.DataID.LocationData.ordinal(), 1000);
		for (UserData pi : profiles){
			com.sendData(pi);
		}			
		
		System.out.println("Done uploading profiles ");
		
		List<UserData> policies = Generator.generatePolicies(0, 1000, false);
		for (UserData pi : policies){
			com.sendData(pi);
		}
		System.out.println("Done uploading policies");
		
		List<FriendInfo> friends = Generator.generateFriends(1000);
		for (FriendInfo fi : friends){
			com.sendData(fi);
		}
		System.out.println("Done uploading friends");
				
		uploadGroupAndPolicies(1000, friends, com,false);
		System.out.println("Done uploading groups");
				
		System.out.println("Done uploading policies");
		
		sendLocations(com);
		//sendVitals(com);
		//sendRequest(com);
	}
	
	private static void uploadGroupAndPolicies(int nUsers, List<FriendInfo> friends, Communicator com, boolean isLatest){
		Map<String, List<String>> friendsMap = new HashMap<String, List<String>>();
				
		for (FriendInfo fi : friends){
			if (!friendsMap.containsKey(fi.getUser1())){
				friendsMap.put(fi.getUser1(), new ArrayList<String>());				
			}
			if (!friendsMap.containsKey(fi.getUser2())){
				friendsMap.put(fi.getUser2(), new ArrayList<String>());				
			}
			friendsMap.get(fi.getUser1()).add(fi.getUser2());
			friendsMap.get(fi.getUser2()).add(fi.getUser1());
		}
		
		for (int i=0; i<nUsers; i++){
			List<GroupInfo> groups = Generator.generateGroup(i,
					friendsMap.get("user_" + i + "@gmail.com"));			
			for (int j=0; j<groups.size(); j++){			
				PolicyInfo pi = Generator.generateGroupPolicies(3, i, j, isLatest);
				com.sendData(groups.get(j));
				com.sendData(pi);
			}
		}
	}	
	
	private static void sendLocations(Communicator com) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		Date startingDate = sdf.parse(DEFAULT_DATE);
		List<LocationInfo> locs = Generator.generateLocationData(1000,
				startingDate, 10, 10);
		for (LocationInfo loc : locs)
			com.sendData(loc);
				
		System.out.println("Done uploading location data");
	}
		
	private static void sendVitals(Communicator com) throws ParseException{	
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		Date startingDate = sdf.parse(DEFAULT_DATE);
		List<VitalSignInfo> locs = Generator.generateVitalSignData(10,
				startingDate, 10, 10);
		for (VitalSignInfo loc : locs)
			com.sendData(loc);
				
		System.out.println("Done uploading vital sign data");
	}
	
	private static void addMoreVitalData(Communicator com, int minInterval,
			int elapsedIntervals, int n) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		Date startingDate = sdf.parse(DEFAULT_DATE);
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		cal.setTime(startingDate);
		cal.add(Calendar.MINUTE, minInterval*elapsedIntervals);
		List<VitalSignInfo> locs = Generator.generateVitalSignData(10,
				cal.getTime(), 10, n);
		for (VitalSignInfo loc : locs)
			com.sendData(loc);
				
		System.out.println("Done uploading more vital sign data");
	}
}
